package com.gg.widget.navigationbar

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.gg.utils.UpperCaseTransform
import com.gg.widget.R
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

/**
 * Creator : GG
 * Date    : 2018/5/15
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
class SearchNavigationBar constructor(params: Builder.SearchNavigationParams) : AbsNavigationBar<SearchNavigationBar.Builder.SearchNavigationParams>(params) {

    override fun bindLayoutId(): Int = R.layout.title_search

    override fun applyViews() {

        if (params.mLeftClickListener != null) {
            setOnClickListener(R.id.back, params.mLeftClickListener)
        }

        val clear = findViewById<View>(R.id.clear)


        val edit = findViewById<EditText>(R.id.edit)
        edit.hint = params.hint

        if (params.upperType) {
            edit.transformationMethod = UpperCaseTransform()
        }
        if (!params.digits.isNullOrBlank())
            edit.keyListener = DigitsKeyListener.getInstance(params.digits)

        edit.inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE

        if (params.maxLength != 0)
            edit.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(params.maxLength))

        RxTextView.textChanges(edit)
                .debounce(500, TimeUnit.MICROSECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    if (params.onTextChangeListener != null) {
                        if (it.length >= params.outLength)
                            params.onTextChangeListener?.textChange(it.toString())
                        else {
                            params.onTextChangeListener?.textChange(null)
                        }
                    }
                }

        RxView.clicks(clear)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    edit.setText("")
                }
    }



    class Builder : AbsNavigationBar.Builder {
        internal var params: SearchNavigationParams

        constructor(context: Context) : super(context) {
            params = SearchNavigationParams(context, null)
        }

        constructor(context: Context, viewGroup: ViewGroup) : super(context, viewGroup) {
            params = SearchNavigationParams(context, viewGroup)
        }

        fun setHint(string: String): Builder {
            params.hint = string
            return this
        }

        fun setClickListener(viewId: Int, listener: View.OnClickListener): Builder {
            params.mViewId = viewId
            params.mClickListener = listener
            return this
        }

        fun setLeftText(text: String): Builder {
            params.mLeftText = text
            return this
        }

        fun setRightText(text: String): Builder {
            params.mRightText = text
            return this
        }

        fun setLeftIcon(imgId: Int): Builder {
            params.mLeftIconId = imgId
            return this
        }

        fun setInputType(type: IntArray): Builder {
            params.inputType = type
            return this
        }

        fun setMaxLength(length: Int): Builder {
            params.maxLength = length
            return this
        }

        fun setTextChange(onTextChangeListener: OnTextChangeListener): Builder {
            params.onTextChangeListener = onTextChangeListener
            return this
        }


        fun setLeftClickListener(listener: View.OnClickListener): Builder {
            params.mLeftClickListener = listener
            return this
        }

        fun setMiddleClickListener(listener: View.OnClickListener): Builder {
            params.mMiddleClickListener = listener
            return this
        }

        fun setRightClickListener(listener: View.OnClickListener): Builder {
            params.mRightClickListener = listener
            return this
        }

        fun setUpper(type: Boolean): Builder {
            params.upperType = type
            return this
        }

        fun setDigits(string: String): Builder {
            params.digits = string
            return this
        }

        fun setOutLength(length: Int): Builder {
            params.outLength = length
            return this
        }

        override fun create(): SearchNavigationBar = SearchNavigationBar(params)

        class SearchNavigationParams(context: Context, parent: ViewGroup?) : AbsNavigationBar.Builder.AbsNavigationParams(context, parent) {
            var mLeftText: String? = null
            var hint: String? = null
            var mRightText: String? = null
            var mLeftClickListener: View.OnClickListener? = null
            var mMiddleClickListener: View.OnClickListener? = null
            var mRightClickListener: View.OnClickListener? = null
            var mViewId: Int = 0
            var mText: String? = null
            var mClickListener: View.OnClickListener? = null
            var mLeftIconId = 0
            var mMiddleIconId = 0
            var mRightIconId = 0
            var onTextChangeListener: OnTextChangeListener? = null
            var inputType: IntArray = intArrayOf()
            var maxLength = 0
            var upperType = false
            var digits: String? = null
            var outLength = 0
        }
    }
}